/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.wizardpager.wizard.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.android.wizardpager.wizard.model.AddressPage;
import com.example.android.wizardpager.wizard.model.Page;
import com.example.goodog.R;
import com.example.goodog.customViews.AddressTextBox;
import com.example.goodog.customViews.GetLocationFromMapButton;
import com.example.goodog.interfaces.ChosenCallback;

public class AddressFragment extends Fragment {
	private static final String ARG_KEY = "key";
	private static final int GET_LOCATION_FROM_MAP_COMMAND = 1;

    private PageFragmentCallbacks mCallbacks;
    private String mKey;
    private Page mPage;
    private String label, tip;
    private AddressTextBox atb;
    private GetLocationFromMapButton glfm;
    private TextView tv;

    public static AddressFragment create(String key) {
        Bundle args = new Bundle();
        args.putString(ARG_KEY, key);

        AddressFragment fragment = new AddressFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public AddressFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        mKey = args.getString(ARG_KEY);
        mPage = mCallbacks.onGetPage(mKey);

        AddressPage addressPage = (AddressPage) mPage;
        label = addressPage.getLabel();
        tip = addressPage.getTip();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_page_empty, container, false);
        ((TextView) rootView.findViewById(android.R.id.title)).setText(mPage.getTitle());

        final LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainLL);
        atb = new AddressTextBox(getActivity(),false);
        glfm = new GetLocationFromMapButton(getActivity(), GET_LOCATION_FROM_MAP_COMMAND, this);
        tv = new TextView(getActivity());
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        atb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        atb.setMinimumWidth(300);
        TextView mNameView = ((TextView) rootView.findViewById(R.id.label));
        mNameView.setText(label);
        atb.setHint(tip);
        atb.callback = new ChosenCallback() {
			
			@Override
			public void NotChosencallbackcall() {
				mPage.getData().putString(Page.TEXT_DATA_KEY, "");
				mPage.getData().putDouble(Page.LAT_DATA_KEY,0);
				mPage.getData().putDouble(Page.LON_DATA_KEY,0);
				tv.setText("");
				mPage.notifyDataChanged();
			}
			
			@Override
			public void Chosencallbackcall()
			{
				mPage.getData().putString(Page.TEXT_DATA_KEY, atb.getText().toString());
				mPage.getData().putDouble(Page.LAT_DATA_KEY, atb.getLatitude());
				mPage.getData().putDouble(Page.LON_DATA_KEY, atb.getLongitude());
				tv.setText(atb.getLatitude()+","+atb.getLongitude());
				mPage.notifyDataChanged();
			}
		};
		glfm.callback = new ChosenCallback() {
			
			@Override
			public void NotChosencallbackcall() {

			}
			
			@Override
			public void Chosencallbackcall()
			{
				mPage.getData().putString(Page.TEXT_DATA_KEY, glfm.getAddressString());
				atb.setText(glfm.getAddressString(),false);
				mPage.getData().putDouble(Page.LAT_DATA_KEY, glfm.getLatitude());
				mPage.getData().putDouble(Page.LON_DATA_KEY, glfm.getLongitude());
				tv.setText("("+glfm.getLatitude()+","+glfm.getLongitude()+")");
				mPage.notifyDataChanged();
			}
		};
		ll.addView(atb);
		ll.addView(glfm);
		ll.addView(tv);
		
		String add = mPage.getData().getString(Page.TEXT_DATA_KEY,"");
		double lat = mPage.getData().getDouble(Page.LAT_DATA_KEY, 0);
		double lon = mPage.getData().getDouble(Page.LON_DATA_KEY, 0);
		
		if(!add.equals("") && !(lat == 0 && lon == 0))
		{
			tv.setText(lat+","+lon);
			atb.setText(add);
		}

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (!(activity instanceof PageFragmentCallbacks)) {
            throw new ClassCastException("Activity must implement PageFragmentCallbacks");
        }

        mCallbacks = (PageFragmentCallbacks) activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }  
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    	if (requestCode == GET_LOCATION_FROM_MAP_COMMAND)
    	{
			glfm.onActivityResult(requestCode, resultCode, data);
		}
    }
    
}
